package com.baseweb.webadmin.core.model.dao.impl;

import com.baseweb.webadmin.core.model.dao.AccountDao;
import com.baseweb.webadmin.core.model.dao.ByCoinsLogDao;
import com.baseweb.webadmin.core.model.mapper.ByCoinsLogMapper;
import com.baseweb.webadmin.core.model.mapper.ByUserVoucherMapper;
import com.baseweb.webadmin.core.model.mapper.ByUserWithdrawAccountMapper;
import com.baseweb.webadmin.core.model.mapper.ByUserWithdrawMapper;
import com.baseweb.webadmin.core.model.pojo.*;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class AccountDaoImpl extends SqlSessionDaoSupport implements AccountDao {

    @Autowired
    ByUserVoucherMapper byUserVoucherMapper;

    @Autowired
    ByUserWithdrawMapper byUserWithdrawMapper;

    @Autowired
    ByUserWithdrawAccountMapper byUserWithdrawAccountMapper;

    @Override
    public Integer insertVoucher(ByUserVoucher byUserVoucher) {
        if (byUserVoucher.getId() !=null && byUserVoucher.getId() > 0){
            return byUserVoucherMapper.updateByPrimaryKey(byUserVoucher);
        }else {
            return byUserVoucherMapper.insert(byUserVoucher);
        }
    }

    @Override
    public ByUserVoucher findVoucher(Integer id) {
        return byUserVoucherMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserVoucher> selectVoucherListByPage(Integer userId, Integer start, Integer offset) {
        ByUserVoucherExample example = new ByUserVoucherExample();
        ByUserVoucherExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserVoucherMapper.selectByExample(example);
    }

    @Override
    public Integer selectVoucherListCount(Integer userId) {
        ByUserVoucherExample example = new ByUserVoucherExample();
        ByUserVoucherExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        return byUserVoucherMapper.countByExample(example);
    }

    @Override
    public Integer insertWithdrawAccount(ByUserWithdrawAccount byUserWithdrawAccount) {
        if (byUserWithdrawAccount.getId() !=null && byUserWithdrawAccount.getId() > 0){
            return byUserWithdrawAccountMapper.updateByPrimaryKey(byUserWithdrawAccount);
        }else {
            return byUserWithdrawAccountMapper.insert(byUserWithdrawAccount);
        }
    }

    @Override
    public ByUserWithdrawAccount findWithdrawAccount(Integer id) {
        return byUserWithdrawAccountMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserWithdrawAccount> selectWithdrawAccountListByPage(Integer userId, Integer start, Integer offset) {
        ByUserWithdrawAccountExample example = new ByUserWithdrawAccountExample();
        ByUserWithdrawAccountExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserWithdrawAccountMapper.selectByExample(example);
    }

    @Override
    public Integer selectWithdrawAccountListCount(Integer userId) {
        ByUserWithdrawAccountExample example = new ByUserWithdrawAccountExample();
        ByUserWithdrawAccountExample.Criteria criteria = example.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        return byUserWithdrawAccountMapper.countByExample(example);
    }

    @Override
    public Integer insertWithdraw(ByUserWithdraw byUserWithdraw) {
        if (byUserWithdraw.getId() !=null && byUserWithdraw.getId() > 0){
            return byUserWithdrawMapper.updateByPrimaryKey(byUserWithdraw);
        }else {
            return byUserWithdrawMapper.insert(byUserWithdraw);
        }
    }

    @Override
    public ByUserWithdraw findWithdraw(Integer id) {
        return byUserWithdrawMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ByUserWithdraw> selectWithdrawListByPage(List<Integer> userIds, Integer start, Integer offset) {
        ByUserWithdrawExample example = new ByUserWithdrawExample();
        ByUserWithdrawExample.Criteria criteria = example.createCriteria();
        if(userIds != null && userIds.size() > 0){
            criteria.andUserIdIn(userIds);
        }
        example.setOrderByClause(String.format("id desc limit %s,%s", start, offset));
        return byUserWithdrawMapper.selectByExample(example);
    }

    @Override
    public Integer selectWithdrawListCount(List<Integer> userIds) {
        ByUserWithdrawExample example = new ByUserWithdrawExample();
        ByUserWithdrawExample.Criteria criteria = example.createCriteria();
        if(userIds != null && userIds.size() > 0){
            criteria.andUserIdIn(userIds);
        }
        return byUserWithdrawMapper.countByExample(example);
    }
}
